<template>
    <TeacherDetail :teacher="teacher" @submit="handleSubmit"/>
</template>

<script setup>
    import TeacherDetail from '../components/TeacherDetail.vue';
    import { useUserStore } from '../stores/user';
    import { useMemberStore } from '../stores/member';
    import { ref, onMounted } from 'vue'
    import { ElMessage } from 'element-plus'

    const userStore = useUserStore()
    const memberStore = useMemberStore()

    const teacher = ref({
      id: null,
      name: '',
      subjects: []
    })

    onMounted(() => {
      const t = memberStore.getUserByUsername(userStore.username)
      if(t) {
        teacher.value.id = t.id
        teacher.value.name = t.name
        teacher.value.subjects = t.subjects
      }
    })

    const handleSubmit = () => {
      memberStore.updateTeacherSubjects(teacher.value.id, teacher.value.subjects)
      ElMessage.success('教学科目已保存')
    }
</script>